package com.bsj.power.system.web;

import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.vo.sys.SysMonitorSearchVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author yinhao
 * @version 1.0
 * @description 系统监控
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/system/monitor")
//@SaCheckPermission("/master/system/monitor")
public class SystemMonitorController {

    public static final Long INTERVAL = 604799L;

    @Value("${prometheus.url}")
    public String url;

//    @Value("${prometheus.greptimedb}")
//    public String greptimedb;
//
//    @Value("${prometheus.username}")
//    public String username;
//
//    @Value("${prometheus.password}")
//    public String password;
//
//    /**
//     * 前缀-GreptimeDB 实现了兼容 Prometheus 的一系列 API
//     */
//    private final String path = "/v1/prometheus/api/v1/query_range?query=";


    /**
     * 接口流量QPS
     *
     * @param searchVO
     * @return
     */
    @GetMapping("/httpQps")
    public JsonResult<String> httpQps(@Validated SysMonitorSearchVO searchVO) {
        Long startTime = Long.parseLong(searchVO.getStartTime());
        Long endTime = Long.parseLong(searchVO.getEndTime());
        String step = "60";
        if (endTime - startTime > INTERVAL) {
            step = "3600";
        }
        // Prometheus 版本
        return JsonResult.success(cn.hutool.http.HttpUtil.get(url + "sum(rate(http_server_requests_seconds_count{application=\"" + searchVO.getApplication() + "\"}[1m]))&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step));

//        String responseBody = HttpUtil.doGetAuth(greptimedb + path + "sum(rate(http_server_requests_seconds_count{application=\"" + searchVO.getApplication() + "\"}[1m]))&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step, username, password);
//        return JsonResult.success(responseBody);
    }

    /**
     * cpu使用率
     *
     * @param searchVO
     * @return
     */
    @GetMapping("/cpuUsage")
    public JsonResult<String> cpuUsage(@Validated SysMonitorSearchVO searchVO) {
        Long startTime = Long.parseLong(searchVO.getStartTime());
        Long endTime = Long.parseLong(searchVO.getEndTime());
        String step = "60";
        if (endTime - startTime > INTERVAL) {
            step = "3600";
        }
        // Prometheus 版本
        return JsonResult.success(cn.hutool.http.HttpUtil.get(url + "system_cpu_usage{application=\"" + searchVO.getApplication() + "\"}&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step));

//        String responseBody = HttpUtil.doGetAuth(greptimedb + path + "system_cpu_usage{application=\"" + searchVO.getApplication() + "\"}&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step, username, password);
//        return JsonResult.success(responseBody);
    }

    /**
     * 堆内存使用率
     *
     * @param searchVO
     * @return
     */
    @GetMapping("/heapMemoryUsed")
    public JsonResult<String> heapMemoryUsed(@Validated SysMonitorSearchVO searchVO) {
        Long startTime = Long.parseLong(searchVO.getStartTime());
        Long endTime = Long.parseLong(searchVO.getEndTime());
        String step = "60";
        if (endTime - startTime > INTERVAL) {
            step = "3600";
        }
        // Prometheus 版本
        return JsonResult.success(cn.hutool.http.HttpUtil.get(url + "sum(jvm_memory_used_bytes{application=\"" + searchVO.getApplication() + "\",+area=\"heap\"})*100/sum(jvm_memory_max_bytes{application=\"" + searchVO.getApplication() + "\",+area=\"heap\"})&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step));

//        String responseBody = HttpUtil.doGetAuth(greptimedb + path + "sum(jvm_memory_used_bytes{application=\"" + searchVO.getApplication() + "\",+area=\"heap\"})*100/sum(jvm_memory_max_bytes{application=\"" + searchVO.getApplication() + "\",+area=\"heap\"})&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step, username, password);
//        return JsonResult.success(responseBody);
    }

    /**
     * 系统运行时长
     *
     * @param searchVO
     * @return
     */
    @GetMapping("/processUptime")
    public JsonResult<String> processUptime(@Validated SysMonitorSearchVO searchVO) {
        Long startTime = Long.parseLong(searchVO.getStartTime());
        Long endTime = Long.parseLong(searchVO.getEndTime());
        String step = "60";
        if (endTime - startTime > INTERVAL) {
            step = "3600";
        }
        // Prometheus 版本
        return JsonResult.success(cn.hutool.http.HttpUtil.get(url + "process_uptime_seconds{application=\"" + searchVO.getApplication() + "\"}&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step));

//        String responseBody = HttpUtil.doGetAuth(greptimedb + path + "process_uptime_seconds{application=\"" + searchVO.getApplication() + "\"}&start=" + searchVO.getStartTime() + "&end=" + searchVO.getEndTime() + "&step=" + step, username, password);
//        return JsonResult.success(responseBody);
    }
}
